class List

	private

		# Right-click on the books list
		def filter(w, e)
			if e.kind_of? Gdk::EventButton and e.button == 3
				path = $glade["livros"].get_path_at_pos(e.x, e.y)
				if path != nil
					$glade["livros"].selection.select_path(path[0])
				else
					$glade["livros"].selection.unselect_all
				end
				if $glade["livros"].selection.selected == nil
					@edit_book.hide
					@separator.hide
					@delete.hide
				else
					@edit_book.show
					@separator.show
					@delete.show
				end
				@popup.popup(nil, nil, e.button, e.time)
			end
		end

		def filter_publisher(name)
			populate_list(@list_store)
			cod_publisher = $db.get_first_value("SELECT cod_publisher 
												   FROM publisher
												  WHERE publisher = '#{name}'").to_i
			r = []
			@list_store.each do |model, path, iter|
				if iter[9] != cod_publisher
					r << iter
				end
			end
			r.each { |iter| @list_store.remove(iter) }
		end

		def filter_author(name)
			populate_list(@list_store)
			cod_author = $db.get_first_value("SELECT cod_author
												FROM author
											   WHERE author LIKE '#{name}'").to_i
			r = []
			@list_store.each do |model, path, iter|
				if iter[10] != cod_author
					r << iter
				end
			end
			r.each { |iter| @list_store.remove(iter) }
		end

		def filter_genre(name)
			populate_list(@list_store)
			cod_genre = $db.get_first_value("SELECT cod_genre
											   FROM genre
											  WHERE genre LIKE '#{name}'").to_i
			r = []
			@list_store.each do |model, path, iter|
				if iter[12] != cod_genre
					r << iter
				end
			end
			r.each { |iter| @list_store.remove(iter) }
		end

		def filter_subgenre(name)
			populate_list(@list_store)
			cod_genre = $db.get_first_value("SELECT cod_genre
											   FROM genre
											  WHERE genre LIKE '#{name}'").to_i
			r = []
			@list_store.each do |model, path, iter|
				if iter[11] != cod_genre
					r << iter
				end
			end
			r.each { |iter| @list_store.remove(iter) }
		end

		def filter_language(name)
			populate_list(@list_store)
			cod_language = $db.get_first_value("SELECT cod_language
											   FROM language
											  WHERE language LIKE '#{name}'").to_i
			r = []
			@list_store.each do |model, path, iter|
				if iter[13] != cod_language
					r << iter
				end
			end
			r.each { |iter| @list_store.remove(iter) }
		end

		def filter_year(range)
			populate_list(@list_store)
			r = []
			@list_store.each do |model, path, iter|
				if not iter[3].between? range.first, range.last
					r << iter
				end
			end
			r.each { |iter| @list_store.remove(iter) }
		end

		def populate_popup
			@popup = nil
			@popup = Gtk::Menu.new
			@edit_book = Gtk::MenuItem.new(_("Edit book"))
			add_book = Gtk::MenuItem.new(_("Add book"))
			author = Gtk::MenuItem.new(_("Author"))
			publisher = Gtk::MenuItem.new(_("Publisher"))
			genre = Gtk::MenuItem.new(_("Genre"))
			subgenre = Gtk::MenuItem.new(_("Subgenre"))
			language = Gtk::MenuItem.new(_("Language"))
			year = Gtk::MenuItem.new(_("Year"))
			remove_filter = Gtk::MenuItem.new(_("Remove Filter"))
			@separator = Gtk::SeparatorMenuItem.new
			@delete = Gtk::MenuItem.new(_("Delete Book"))
			@popup.append(@edit_book)
			@popup.append(add_book)
			@popup.append(Gtk::SeparatorMenuItem.new)
			@popup.append(author)
			@popup.append(publisher)
			@popup.append(genre)
			@popup.append(subgenre)
			@popup.append(language)
			@popup.append(year)
			@popup.append(remove_filter)
			@popup.append(@separator)
			@popup.append(@delete)

			# Add book
			add_book.signal_connect("activate") { |w| @book.show }
			
			# Remove filter
			remove_filter.signal_connect("activate") { |w| populate_list(@list_store) }

			# Delete book
			@delete.signal_connect("activate") do |w|
				iter = $glade["livros"].selection.selected
				d = Gtk::MessageDialog.new($glade["main_window"],
									   Gtk::Dialog::MODAL,
									   Gtk::MessageDialog::WARNING,
									   Gtk::MessageDialog::BUTTONS_YES_NO,
									   _("Delete book?"))
				if iter[2] != nil and iter[2] != ''
					d.secondary_text = _("Are you sure you want to delete the book") + " \"#{iter[1]}\", " + _("from") + " #{iter[2]}?"
				else
					d.secondary_text = _("Are you sure you want to delete the book") + " \"#{iter[1]}\"?"
				end
				d.default_response = Gtk::Dialog::RESPONSE_NO
				d.run do |r| 
					if r == Gtk::Dialog::RESPONSE_YES
						$db.execute("DELETE FROM book WHERE cod_book = #{iter[0]}")
						cleanup_database
						begin
							File.delete("#{BOOKIMGS}/#{iter[0]}.jpg")
							File.delete("#{BOOKIMGS}/#{iter[0]}s.jpg")
						rescue
						end
						populate_list(@list_store)
					end
					d.destroy
				end
			end

			# Author
			mauthor = Gtk::Menu.new
			[('A'..'D'), ('E'..'H'), ('I'..'L'), ('M'..'O'), ('P'..'S'), ('T'..'Z')].each do |rg|
				item = Gtk::MenuItem.new("#{rg.first} - #{rg.last}")
				mauthor.append(item)
				itemmenu = Gtk::Menu.new
				sql = "SELECT DISTINCT author
								  FROM author
								 WHERE cod_author != 0 AND ("
				rg.each { |l| sql += "author LIKE '#{l}%' OR " }
				sql.chomp!(" OR ")
				sql += ") ORDER BY author"
				r = $db.execute(sql)
				r.each do |row|
					subitem = Gtk::MenuItem.new(row[0])
					subitem.signal_connect("activate") do |w|
						filter_author(w.child.text)
					end
					itemmenu.append(subitem)
				end
				item.submenu = itemmenu
			end
			author.submenu = mauthor

			# Publisher
			mpublisher = Gtk::Menu.new
			r = $db.execute("SELECT DISTINCT publisher 
                                        FROM publisher 
                                       WHERE cod_publisher != 0 
                                    ORDER BY publisher")
			r.each do |row|
				item = Gtk::MenuItem.new(row[0])
				item.signal_connect("activate") do |w|
					filter_publisher(w.child.text)
				end
				mpublisher.append(item)
			end
			publisher.submenu = mpublisher

			# Subgenres
			mgenre = Gtk::Menu.new
			r = $db.execute("SELECT DISTINCT cod_genre, genre
									    FROM genre
							           WHERE cod_genre != 0
							             AND (father = '' OR father IS NULL)
								    ORDER BY genre")
			r.each do |row|
				item = Gtk::MenuItem.new(row[1])
				item.signal_connect("activate") do |w|
					filter_genre(w.child.text)
				end
				mgenre.append(item)
			end
			genre.submenu = mgenre

			# Subgenres
			msubgenre = Gtk::Menu.new
			r = $db.execute("SELECT DISTINCT cod_genre, genre
									    FROM genre
							           WHERE cod_genre != 0
							             AND (father = '' OR father IS NULL)
								    ORDER BY genre")
			r.each do |row|
				item = Gtk::MenuItem.new(row[1])
				itemmenu = Gtk::Menu.new
				s = $db.execute("SELECT DISTINCT genre
								            FROM genre
								           WHERE father = #{row[0]}
							            ORDER BY genre")
				s.each do |rr|
					subitem = Gtk::MenuItem.new(rr[0])
					subitem.signal_connect("activate") do |w|
						filter_subgenre(w.child.text)
					end
					itemmenu.append(subitem)
				end
				item.submenu = itemmenu
				msubgenre.append(item)
			end
			subgenre.submenu = msubgenre

			# Language
			mlanguage = Gtk::Menu.new
			r = $db.execute("SELECT DISTINCT language 
                                        FROM language
                                       WHERE cod_language != 0 
                                    ORDER BY language")
			r.each do |row|
				item = Gtk::MenuItem.new(row[0])
				item.signal_connect("activate") do |w|
					filter_language(w.child.text)
				end
				mlanguage.append(item)
			end
			language.submenu = mlanguage

			# Year
			myear = Gtk::Menu.new
			[
				[_("Antiquity"), (-6000)..476],
				[_("Middle Ages"), 477..1453],
				[_("Modern Age"), 1454..1799],
				[_("19th Century"), 1800..1899],
				[_("1900s - 1940s"), 1900..1949],
				[_("1950s"), 1950..1959],
				[_("1960s"), 1960..1969],
				[_("1970s"), 1970..1979],
				[_("1980s"), 1980..1989],
				[_("1990s"), 1990..1999],
				[_("2000s and beyond"), 2000..9999]
			].each do |rg|
				item = Gtk::MenuItem.new(rg[0])
				item.signal_connect("activate") do |w|
					filter_year(rg[1])
				end
				myear.append(item)
			end
			year.submenu = myear

			###

			@popup.show_all
		end

end
